<?php
/*
 * @author zhufeng
 * 
 */
class TemaController extends AppController
{				
	var $name = 'Tema';
	var $uses = array('SysParams','Company','CompanyGroup','CompanysCompanyGroups');
	var $access = array ('index'=>array('role' => array('System')),
      'areakind'=>array('role' => array('System')),
      'kind'=>array('role' => array('System')),
      'addkind'=>array('role' => array('System')),
      'addarea'=>array('role' => array('System')),
      'doaddarea'=>array('role' => array('System')),
      'doaddkind'=>array('role' => array('System')),
      'deleteArea'=>array('role' => array('System')),
      'dodelete'=>array('role' => array('System')),
      'confirmArea'=>array('role' => array('System')),
      'confirmKind'=>array('role' => array('System')),
      'doarea'=>array('role' => array('System')),
      'dokind'=>array('role' => array('System')),
      'setValue'=>array('role' => array('System')),
      'company'=>array('role' => array('System')),
      'setAreaValue'=>array('role' => array('System'))
      );
	function index()
	{
	}
	//@author zhufeng
	function areakind()
	{
		$order=" order by id";
		$condition="\"SysParams\".\"kbn\"='area_id' and \"SysParams\".\"deleted\" is null";
		$data = $this->SysParams->findAll($condition,null,$order,$this->show,$this->page);
		$this->setAreaValue($data);
		$count = $this->SysParams->findCount($condition);
		
		$this->setPaging('ajax','/tema/areakind',$count);
		
		$this->set('role',$this->rdAuth->role);
		$this->set('data',$data);
	}
	// @author zhufeng to kind页面
	function kind()
	{
		
		$order=" order by id";
		$condition="\"SysParams\".\"kbn\"='tema_id' and \"SysParams\".\"deleted\" is null";
		$data = $this->SysParams->findAll($condition,null,$order,$this->show,$this->page);
		
		$this->setValue($data);
		
		$count = $this->SysParams->findCount($condition);
		
		$this->setPaging('ajax','/tema/kind',$count);
		
		$this->set('role',$this->rdAuth->role);
		$this->set('data',$data);
		
	}
	//@author yanmin to company page
	function company(){
		$order=" order by id desc";
		
		$condition="\"CompanyGroup\".\"deleted\" is null";
		
		$data=$this->CompanyGroup->findAll($condition,null,$order,$this->show,$this->page);
		
		$this->setCompanyGroupValue($data);
		
		$count = $this->CompanyGroup->findCount($condition);
		
		$this->setPaging('ajax','/tema/company',$count);
		
		$this->set('role',$this->rdAuth->role);
		$this->set('data',$data);
	}
	
	////@author zhufeng to addkind页面
	function addkind()
	{
	}	
	function addarea()
	{
		
	}
	function addcompany()
	{
		
	}
	function company_list($company_flg){
		$this->layout = 'ajax'; 
		$company_data = $this->Company->generateList("\"Company\".\"deleted\" is null and \"Company\".\"company_type_id\"=1",null,null,'{n}.Company.id', '{n}.Company.company_name');
		$this->set('companydata',$company_data);
		$this->set('flg',$company_flg);
	}	
	
	function doaddcompany(){
		$message=$this->Valid->ErrorMsg($this->data,'CompanyGroup/name','required');
		$message.=$this->Valid->ErrorMsg($this->data,'CompanyGroup/name','maxlength','50');
		if(empty($message)){
			$this->CompanyGroup->save($this->data);
			
			$company_id_array = $this->data['CompanyGroup']['company_id'];
			
			if(!empty($company_id_array)){
			$strb = split(",",$company_id_array);
				foreach($strb as $s){
					$company_group_id = $this->CompanyGroup->getLastInsertId();
					$this->data['CompanysCompanyGroups']['company_group_id']=$company_group_id;
					$this->data['CompanysCompanyGroups']['company_id']=$s;
					$this->CompanysCompanyGroups->save($this->data['CompanysCompanyGroups']);
					$data=$this->data;
			        $check=$this->SysParams->checkGroup($this->data);
					if($check){
						$this->SysParams->dosavegroup($this->data);
						$dir="category_mst/group/".$this->data['CompanysCompanyGroups']['company_group_id'];
			            $this->_creatDir($dir);
						$this->redirect('/tema/company');
					    
					}else{
						$message.=$this->Valid->tagErrorMsg('group');
				        $this->set("message",$message);
				        //$this->data['SysParams']['name']=$this->data['SysParams']['area'];
				        $this->set("confirmKind",$this->data);
				        $this->render('addcompany');
				        exit;
					}
				}
			}
			
			$this->redirect("/tema/company");
		}else{
			$this->set("message",$message);
			$this->set("confirmKind",$this->data);
			$this->render('addcompany');
			exit;
		}
	}
	
	////@author zhufeng 处理添加area
	function doaddarea()
	{
		$message=$this->Valid->ErrorMsg($this->data,'SysParams/area','required');
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/area','mbmaxlength',30);
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/description','required');
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/title','required');
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/description','mbmaxlength',20);
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/description','str_number');
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/title','mbmaxlength',50);
		if(empty($message))
		{
			$data=$this->data;
			$check=$this->SysParams->checkArea($this->data);
			if($check)
			{
			$this->SysParams->dosavearea($this->data);
			$dir="category_mst/area/".$data['SysParams']['description'];
			$this->_creatDir($dir);
			$this->redirect("/tema/areakind");
			exit;
			}
			else{
				$message.=$this->Valid->tagErrorMsg('area');
				$this->set("message",$message);
				$this->data['SysParams']['name']=$this->data['SysParams']['area'];
				$this->set("confirmKind",$this->data);
				$this->render('addarea');
				exit;
			}
		}
		else
		{
			$this->set("message",$message);
			$this->data['SysParams']['name']=$this->data['SysParams']['area'];
			$this->set("confirmKind",$this->data);
			$this->render('addarea');
			exit;
		}
	}
	////@author zhufeng 处理添加テーマ
	function doaddkind()
	{
		$message=$this->Valid->ErrorMsg($this->data,'SysParams/name','required');
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/name','mbmaxlength',30);
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/description','required');
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/title','required');
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/description','mbmaxlength',20);
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/description','str_number');
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/title','mbmaxlength',50);
		if(empty($message))
		{
			
			$data=$this->data;
			$check=$this->SysParams->check($this->data);
			if($check)
			{
			$this->SysParams->dosave($this->data);
			$dir="category_mst/tema/".$data['SysParams']['description'];
			
			$this->_creatDir($dir);
			$this->redirect("/tema/kind");
			exit;
			}
			else
			{
				$message.=$this->Valid->tagErrorMsg('tema');
				$this->set("message",$message);
				$this->set("confirmKind",$this->data);
				$this->render('addkind');
				exit;
			}
		}
		else
		{
			$this->set("message",$message);
			$this->set("confirmKind",$this->data);
			$this->render('addkind');
		}
	}	
	//@author yanmin del companygroup
	function delcompanygroup(){
		$delete = array();
		if(isset($_COOKIE['Company_group_delete']))
		{
			$delete = split(',',$_COOKIE['Company_group_delete']);		
			$str = implode(',',$delete);
			$sql="update company_group_mst set deleted=now() "
			."where id in ($str)";
			$this->CompanyGroup->query($sql);	
			setcookie("Company_group_delete","");
			$this->redirect("/tema/company");
			exit;
		}
	}
	
	
	////@author zhufeng 删除area
	function deleteArea()
	{
		$delete = array ();
		if (isset ($_COOKIE['Sys_params_area_delete']))
		{
			$delete = split(',', $_COOKIE['Sys_params_area_delete']);
			$str=implode(',',$delete);
			$this->SysParams->doDeleteArea($str);
			setcookie("Sys_params_area_delete","");
			$this->redirect("/tema/areakind");
			exit;
		}
	}
	////@author zhufeng 删除テーマ
	function dodelete()
	{
		$delete = array ();
		if (isset ($_COOKIE['Sys_params_delete']))
		{
			$delete = split(',', $_COOKIE['Sys_params_delete']);
			$str=implode(',',$delete);
			$this->SysParams->doDelete($str);
			setcookie("Sys_params_delete","");
			$this->redirect("/tema/kind");
			
			exit;
		}
	}
	function confirmCompany($data=null){
		$p=$this->CompanyGroup->find("deleted is null and id= ".$data);
		$this->data = $p;
		$companyid = $this->CompanysCompanyGroups->findAll("company_group_id =".$data);
		$str="";
		if(!empty($companyid)){
			foreach($companyid as $obj){
				$str.= $obj['CompanysCompanyGroups']['company_id'].",";
			}
			//$companyids = implode("",",");
			
		}
		$this->set("companyids",$str);
		$this->set("confirmCompany",$p);
		$this->render("confirmcompany");
		exit;
	}
	////@author zhufeng to 编辑area页面
	function confirmArea($data=null)
	{
		$p=$this->SysParams->find(" kbn = 'area_id' and deleted is null and id = ".$data);
		
		$this->data=$p;
		$p['SysParams']['area']=$p['SysParams']['name'];
		$this->set("confirmKind",$p);
		
		$this->render("confirmarea");
		exit;
	}
	//@author zhufeng to 编辑テーマ页面
	function confirmKind($data=null)
	{
		$p=$this->SysParams->find(" kbn = 'tema_id' and deleted is null and id = ".$data);
		
		$this->data=$p;
		$this->set("confirmKind",$p);
		$this->render("confirm");
		exit;
	}
	
	function docompany(){
		$message=$this->Valid->ErrorMsg($this->data,'CompanyGroup/name','required');
		$message.=$this->Valid->ErrorMsg($this->data,'CompanyGroup/name','maxlength','50');
		
		if(empty($message)){
			$this->CompanyGroup->doupdate($this->data);
			$this->CompanysCompanyGroups->query("delete from companys_companygroups where company_group_id =". $this->data['CompanyGroup']['id']);
			$company_id_array = $this->data['CompanyGroup']['company_id'];
			
			if(!empty($company_id_array)){
			$strb = split(",",$company_id_array);
				foreach($strb as $s){
					$company_group_id = $this->CompanyGroup->getLastInsertId();
					$this->data['CompanysCompanyGroups']['company_group_id']=$this->data['CompanyGroup']['id'];
					$this->data['CompanysCompanyGroups']['company_id']=$s;
					$this->CompanysCompanyGroups->save($this->data['CompanysCompanyGroups']);
				}
			}	
			
			$this->redirect("/tema/company");
			
		}else{
			$this->set("message",$message);
			$this->confirmCompany($this->data['CompanyGroup']['id']);
			$this->render('confirmcompany');
			exit;
		}
		
	}
	//@author zhufeng处理area编辑
	function doarea()
	{
		$message=$this->Valid->ErrorMsg($this->data,'SysParams/area','required');
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/area','mbmaxlength',30);
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/description','required');
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/title','required');
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/description','mbmaxlength',20);
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/description','str_number');
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/title','mbmaxlength',50);
		if(empty($message))
		{
			
			$data=$this->data;
			$check=$this->SysParams->checkConfirmArea($this->data);
			
			if($check)
			{
			$dir="category_mst/area/".$data['SysParams']['description'];
			
			$this->_creatDir($dir);	
			$this->SysParams->doupdateArea($this->data);
			$order=" order by id";
		$condition="\"SysParams\".\"kbn\"='area_id' and \"SysParams\".\"deleted\" is null";
		$data = $this->SysParams->findAll($condition,null,$order,$this->show,$this->page);
		$this->setAreaValue($data);
		$count = $this->SysParams->findCount($condition);
		
		$this->setPaging('ajax','/tema/areakind',$count);
		
		$this->set('role',$this->rdAuth->role);
		$this->set('data',$data);
			$this->render("areakind");
			exit;
			}
			else
			{
				$message.=$this->Valid->tagErrorMsg('area');
				$this->set("message",$message);
				$this->data['SysParams']['name']=$this->data['SysParams']['area'];
				$this->set("confirmKind",$this->data);
				$this->render('confirmarea');
				exit;
			}
			
		}
		else
		{
			$this->set("message",$message);
			$this->data['SysParams']['name']=$this->data['SysParams']['area'];
			$this->set("confirmKind",$this->data);
			pr($this->data);
			$this->render("confirmarea");
			exit;
		}
	}
	////@author zhufeng 处理テーマ编辑
	function dokind()
	{
		$message=$this->Valid->ErrorMsg($this->data,'SysParams/name','required');
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/name','mbmaxlength',30);
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/description','required');
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/title','required');
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/description','mbmaxlength',20);
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/description','str_number');
		$message.=$this->Valid->ErrorMsg($this->data,'SysParams/title','mbmaxlength',50);
		
		if(empty($message))
		{
			
			$data=$this->data;
			$check=$this->SysParams->checkConfirm($this->data);
			if($check)
			{
			pr($data['SysParams']['description']);
			$dir="category_mst/tema/".$data['SysParams']['description'];
			
			$this->_creatDir($dir);
                         ">>>>>".$dir;
			$this->SysParams->doupdate($this->data);
			$order=" order by id";
		$condition="\"SysParams\".\"kbn\"='tema_id' and \"SysParams\".\"deleted\" is null";
		$data = $this->SysParams->findAll($condition,null,$order,$this->show,$this->page);
		
		$this->setValue($data);
		$count = $this->SysParams->findCount($condition);
		
		$this->setPaging('ajax','/tema/kind',$count);
		
		$this->set('role',$this->rdAuth->role);
		$this->set('data',$data);
			$this->render("kind");
			exit;
			}
			else
			{
				$message.=$this->Valid->tagErrorMsg('tema');
				$this->set("message",$message);
				$this->set("confirmKind",$this->data);
				$this->render('confirm');
				exit;
			}
		}
		else
		{
			$this->set("message",$message);
			
			$this->set("confirmKind",$this->data);
			$this->render("confirm");
			exit;
		}
	}
	////@author zhufeng 保存到cookie
	function setValue(& $data) {

		$delete = array ();

		if (isset ($_COOKIE['Sys_params_delete']))
			$delete = split(',', $_COOKIE['Sys_params_delete']);
		foreach ($data as & $d) {
			$id = $d['SysParams']['id'];
			if (in_array($id, $delete)) {
				$d['SysParams']['delete'] = $id;
			}
		}
	}
	//@author zhufeng
	function setAreaValue(& $data)	
	{
		$delete = array ();

		if (isset ($_COOKIE['Sys_params_area_delete']))
			$delete = split(',', $_COOKIE['Sys_params_area_delete']);

		foreach ($data as & $d) {
			$id = $d['SysParams']['id'];
			if (in_array($id, $delete)) {
				$d['SysParams']['del'] = $id;
			}
		}
	}
	
	//@author zhufeng
	function setCompanyGroupValue(& $data)	
	{
		$delete = array ();

		if (isset ($_COOKIE['Company_group_delete']))
			$delete = split(',', $_COOKIE['Company_group_delete']);
		foreach ($data as & $d) {
			$id = $d['CompanyGroup']['id'];
			if (in_array($id, $delete)) {
				$d['CompanyGroup']['del'] = $id;
			}
		}
	}
}

?>